package tool;

import org.junit.Test;

import java.io.*;

/**
 * @ClassName IOUtil
 * @Description TODO
 * @Author LYJ
 * @Date 2018/12/23 16:46
 * @Verison 1.0
 **/
public class IOUtil {


    /*
    单字节copy
     */
    public void copyFileByByte(File srcFile,File destFile){
        if(!srcFile.exists()){
            throw new IllegalArgumentException("文件"+srcFile+"不存在");
        }

        if(!srcFile.isFile()){
            throw new IllegalArgumentException(destFile+"不是文件");
        }

        try {
            FileInputStream inputStream = new FileInputStream(srcFile);
            FileOutputStream outputStream = new FileOutputStream(destFile);
            int read;
            while ((read = inputStream.read())!=-1){
                outputStream.write(read);
            }
            inputStream.close();
            outputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    /*
    批量copy
     */
    public void copyFileByArray(File srcFile,File destFile){
        if(!srcFile.exists()){
            throw new IllegalArgumentException("文件"+srcFile+"不存在");
        }

        if(!srcFile.isFile()){
            throw new IllegalArgumentException(destFile+"不是文件");
        }
        try {
            FileInputStream inputStream = new FileInputStream(srcFile);
            FileOutputStream outputStream = new FileOutputStream(destFile);

            byte[] bytes = new byte[8192];
            int read;
            while((read = inputStream.read(bytes))!=-1){
                outputStream.write(bytes,0,read);
                outputStream.flush();
            }
            inputStream.close();
            outputStream.close();

        } catch (IOException e) {
            e.printStackTrace();
        }


    }
    /*
    缓冲copy
     */
    public void fileCopyByBuffer(File srcFile,File destFile){
        if(!srcFile.exists()){
            throw new IllegalArgumentException("文件"+srcFile+"不存在");
        }

        if(!srcFile.isFile()){
            throw new IllegalArgumentException(destFile+"不是文件");
        }

        try {
            BufferedInputStream inputStream = new BufferedInputStream(new FileInputStream(srcFile));
            BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(destFile));
            int read;
            byte[] bytes = new byte[8192];
            while ((read = inputStream.read(bytes))!=-1){
                outputStream.write(bytes,0,read);
            }
            outputStream.flush();
            inputStream.close();
            outputStream.close();

        } catch (IOException e) {
            e.printStackTrace();
        }

    }




}
